package com.yunzhou.utils;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author	Huangwm
 * @description	实物资产管理应用中使用的后台数据库操作方法
 */
public class DBTools {

	private LogTool logTool = new LogTool("/log/huangwm/utils/DBTools",false);
	/**
	 * 根据用户id获取部门id
	 * @param userid
	 * @return departmentid
	 */
	public  String getDepid(String userid){
		if(userid.equals("") || userid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select departmentid from hrmresource where id="+userid);
		logTool.writeLog("DBTools getDepid---> select departmentid from hrmresource where id="+userid);
		if(rs.next()){
			return rs.getString("departmentid");
		}else{
			return "0";
		}
	}
	
	/**
	 * 根据用户id获取部门名称
	 * @param userid
	 * @return departmentname
	 */
	public  String getDepName(String userid){
		if(userid.equals("") || userid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select departmentname from HrmDepartment where id = (select subcompanyid1 from HrmResource where id="+userid+")");
		logTool.writeLog("DBTools getDepName---> select departmentname from HrmDepartment where id = (select subcompanyid1 from HrmResource where id="+userid+")");
		if(rs.next()){
			return rs.getString("departmentname");
		}else{
			return "null";
		}
	}

	/**
	 * 根据部门id获取部门名称
	 * @param depid
	 * @return departmentname
	 */
	public  String getDepNameById(String depid){
		if(depid.equals("") || depid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select departmentname from HrmDepartment where id ="+depid);
		logTool.writeLog("DBTools getDepNameById--> select departmentname from HrmDepartment where id ="+depid);
		if(rs.next()){
			return rs.getString("departmentname");
		}else{
			return "null";
		}
	}
	
	/**
	 * 根据用户id获取分部id
	 * @param userid
	 * @return subcompanyid1
	 */
	public  String getSubid(String userid){
		if(userid.equals("") || userid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select subcompanyid1 from hrmresource where id="+userid);
		logTool.writeLog("DBTools getSubid---> select subcompanyid1 from hrmresource where id="+userid);
		if(rs.next()){
			return rs.getString("subcompanyid1");
		}else{
			return "0";
		}
	}
	
	/**
	 * 根据用户id获取分部名称
	 * @param userid
	 * @return subcompanyname
	 */
	public  String getSubName(String userid){
		if(userid.equals("") || userid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select subcompanyname from HrmSubCompany where id = (select subcompanyid1 from HrmResource where id="+userid+")");
		logTool.writeLog("DBTools getSubName---> select subcompanyname from HrmSubCompany where id = (select subcompanyid1 from HrmResource where id="+userid+")");
		if(rs.next()){
			return rs.getString("subcompanyname");
		}else{
			return "null";
		}
	}

	/**
	 * 根据部门id获取分部名称
	 * @param subid
	 * @return subcompanyname
	 */
	public  String getSubNameById(String subid){
		if(subid.equals("") || subid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select subcompanyname from HrmSubCompany where id = "+subid);
		logTool.writeLog("DBTools getSubNameById---> select subcompanyname from HrmSubCompany where id = "+subid);
		if(rs.next()){
			return rs.getString("subcompanyname");
		}else{
			return "0";
		}
	}

	/**
	 * 根据用户id获取姓名
	 * @param userid
	 * @return lastname
	 */
	public  String getLastname(String userid){
		if(userid.equals("1")){
			return "系统管理员";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select lastname from hrmresource where id="+userid);
		logTool.writeLog("DBTools getLastname---> select lastname from hrmresource where id = "+userid);
		if(rs.next()){
			return rs.getString("lastname");
		}else{
			return "null";
		}
	}
	
	/**
	 * 根据名获取用户id
	 * @param name
	 * @return userid
	 */
	public String getUserid(String name){
		RecordSet rs = new RecordSet();
		rs.execute("select id from hrmresource where lastname = '%"+name+"%'");
		logTool.writeLog("DBTools getUserid---> select id from hrmresource where lastname = '%"+name+"%'");
		if(rs.next()){
			return rs.getString("id");
		}else{
			return "0";
		}
	}
	
	/**
	 * 根据工号获取用户id
	 * @param code
	 * @return userid
	 */
	public String getUseridByCode(String code){
		RecordSet rs = new RecordSet();
		rs.execute("select id from hrmresource where workcode = '"+code+"'");
		logTool.writeLog("DBTools getUseridByCode---> select id from hrmresource where workcode = '"+code+"'");
		if(rs.next()){
			return rs.getString("id");
		}else{
			return "0";
		}
	}
	
	/**
	 * 生成UUID
	 * @return
	 */
	public  String getUUID(){
	    String uuid = UUID.randomUUID().toString().toLowerCase();
	    return uuid;
	}

	/**
	 * 根据建模表名和uuid查找对应的数据id
	 * @param tablename
	 * @param uuid
	 * @return
	 */
	public String getBillid(String tablename,String uuid){
		String id = "";
		RecordSet rs = new RecordSet();
		String sql = "select * from "+tablename+" where uuid='"+uuid+"'";
		rs.execute(sql);
		logTool.writeLog("DBTools getBillid---> "+sql);
		if(rs.next()) {
			return  rs.getString("id");
		}
		return id;
	}

	/**
	 * 表单建模权限重构方法，重构后才能在台账里查看到数据
	 * @param creater 创建人
	 * @param formmodeid 模块id
	 * @param billid 数据id,即对应uf表里的id
	 */
	public void refreshRight( int creater, int formmodeid, int billid){
		try{
			ModeRightInfo modeRightInfo = new ModeRightInfo();
			//console.info("当前重构数据：creater=" + creater + "  formmodeid=" + formmodeid + "  billid=" + billid);
			modeRightInfo.setNewRight(true);
			modeRightInfo.editModeDataShare(creater, formmodeid, billid);
			modeRightInfo.addDocShare(creater, formmodeid, billid);
		}
		catch (Exception e){
			logTool.writeLog("DBTools refreshRight建模权限重构代码报错：creater=" + creater + "  formmodeid=" + formmodeid + "  billid=" + billid);
			logTool.writeLog(e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取流程创建时间
	 * @param requestid
	 * @return
	 */
	public String GetWorkflowCreatetime(String requestid){	
		RecordSet rs = new RecordSet();
		String WorkflowCreatetime = "";
		if(!"".equals(requestid)){
			rs = new RecordSet();
			String sql = "select createdate,createtime from workflow_requestbase where requestid="+requestid;
			rs.execute(sql);
			if(rs.next())
				WorkflowCreatetime = Util.null2String(rs.getString("createdate")+" "+rs.getString("createtime"));	
		}
		return WorkflowCreatetime;
	}
	
	/**
	 * 根据requestid获取对应表单tablename
	 * @param requestid
	 * @return
	 */
	public String getBillTableName(String requestid){
		RecordSet rs = null;
		String tablename = "";
		if(!"".equals(requestid)){
			rs = new RecordSet();
			String sql = " select wb2.tablename from workflow_requestbase wr left join workflow_base wb1 on wb1.id=wr.workflowid"+
					"\n left join workflow_bill wb2 on wb1.formid=wb2.id where requestid="+requestid;			
			rs.execute(sql);
			if(rs.next()) tablename = rs.getString("tablename");
		}
		return tablename;
	}

	/**
	 * 根据requestid获取对应表单tablename
	 * @param requestid
	 * @return
	 */
	public String getRequestName(String requestid){
		RecordSet rs = null;
		String requestname = "";
		if(!"".equals(requestid)){
			rs = new RecordSet();
			String sql = " select requestname from workflow_requestbase  where requestid="+requestid;
			rs.execute(sql);
			if(rs.next()) requestname = rs.getString("requestname");
		}
		return requestname;
	}

	/**
	 * 获取部门矩阵某列的人员信息
	 * @param field
	 * @return
	 */
	public String getMatrix2(String field) throws Exception {
		if (field.isEmpty()) throw new Exception("获取部门矩阵人员信息>field is null");
		List<String> list = new ArrayList<>();
		RecordSet rs = new RecordSet();
		String sql = "select "+field+" from Matrixtable_2 where "+field+" is not null ";
		if(rs.execute(sql)){
			while (rs.next()){
				if(!list.contains(rs.getString(field)) && !rs.getString(field).isEmpty()){
					list.add(rs.getString(field));
				}
			}
		}else{
			throw new Exception("获取部门矩阵人员信息>sql fail("+sql+")");
		}
		logTool.writeLog(list.toString().replace("[","").replace("]",""));
		return list.toString().replace("[","").replace("]","");
	}

	/**
	 * 获取角色内的所有用户--包含部门和分部和角色内的用户
	 * @return
	 */
	public String getRoleMembers(){
		return "";
	}
}
